<template>
  <div class="like">
    <div class="goods-list">
      <div
        v-for="(item,index) in likeList"
        :key="index"
        class="goods-item"
        @click="todetail(item.goods)"
      >
        <div class="goods-img">
          <img
            :src="item.goods.defaultAlbum"
            alt=""
          >
        </div>
        <div class="goods-info">
          <div class="name">
            {{ item.goods.cProductShowName }}
          </div>
          <div class="price">
            ￥{{ item.goods.fSalePrice }}
          </div>
        </div>
      </div>
    </div>
    <div
      v-if="lastPage > page"
      class="more"
      @click="getMore"
    >
      点击加载更多+
    </div>
    <div
      v-else
      class="more"
    >
      没有更多
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      likeList: [],
      lastPage: 1, // 总页数
      page: 1,
    };
  },
  mounted() {},
  methods: {
    //获取列表
    getData() {
      let params = {
        limit: 2,
        page: this.page,
        shop_id: sessionStorage.getItem("ShopID")
      }
      this.$api.commit.GetShopHotGoods(params).then(res =>{
        console.log('asdasda',res.data, res.data.last_page)
        this.likeList = this.likeList.concat(res.data.data); // 获取符合要求的商品id
        this.GetGoodsShowData('','','','', this.likeList, res.data.last_page)
      })
    },
    // 获取商品详情
    /**
     * regionId: '', // 地区id
     * classifyId: "", // 分类
     * areaId: "", // 排序
     * keywords: "", // 搜索
     * arrdata：[], // 分页后的数据
     */
    GetGoodsShowData(areaId, classifyId, regionId, keywords, arrdata, lastPage) {
      console.log('arrdata', arrdata, lastPage)
      this.lastPage = lastPage
      // let goodData = {
      //   queryCondition: {
      //     order: [
      //       {
      //         fieldname: areaId, // 排序方式
      //         direction: "asc",
      //       },
      //     ],
      //     pageindex: 1,
      //     pagesize: 100000000000,
      //     where: [
      //       {
      //         fieldname: "categoryid", // 商品分类id
      //         valuefrom: classifyId,
      //       },
      //       {
      //         fieldname: "area_id", // 区域id
      //         valuefrom: regionId,
      //       },
      //       {
      //         fieldname: "keyword", // 搜索关键词
      //         valuefrom: keywords,
      //       },
      //       {
      //         fieldname: "goods_id", // 商品id
      //         valuefrom: goods_id,
      //       },
      //     ],
      //   },
      // }
      // this.$api.commit.GetProductList(goodData).then(element => {
      //   // this.$set(item, "goods", element.data.data[0]);
      //   console.log('element.data.data[0]', element.data.data[0])
      //   return element.data.data[0]
      // })
    },
    // 筛选
    ScreeningData(ScreeningData) {
      console.log('筛选子组件', ScreeningData)
      // 通过分类筛选列表
      let params = {
        shop_id: sessionStorage.getItem("ShopID")
      }
      let NewDataArr = []
      this.$api.commit.GetShopHotGoods(params).then(res =>{
        res.data.data.forEach(goodsid =>{
          let goodData = {
            queryCondition: {
              order: [
                {
                  fieldname: ScreeningData.areaId,
                  direction: "asc",
                },
              ],
              pageindex: this.page,
              pagesize: 10000,
              where: [
                {
                  fieldname: "categoryid", // 商品分类id
                  valuefrom: ScreeningData.classifyId,
                },
                {
                  fieldname: "area_id", // 区域id
                  valuefrom: ScreeningData.regionId,
                },
                {
                  fieldname: "keyword", // 搜索关键词
                  valuefrom: ScreeningData.keywords,
                },
                {
                  fieldname: "goods_id", // 商品id
                  valuefrom: goodsid.goods_id,
                },
              ],
            },
          }
          this.$api.commit.GetProductList(goodData).then(element => {
            if(element.code === 200) {
              if(element.data.data.length > 0) {
                goodsid.goods = element.data.data[0]
                NewDataArr.push(goodsid)
              } else {
                return false
              }
            } else {
              this.$message.error(element.message);
            }
            console.log('NewDataArr', NewDataArr)
            this.likeList = NewDataArr // 输出搜索结果
          })
        })
      })
    },
    // 跳转商品详情
    todetail(item) {
      this.$router.push({
        path: "/fruit/detail",
        query: {
          id: item.id,
          iShopId: item.iShopId
        },
      });
    },
    // 加载更多
    getMore() {
      this.page++;
      this.getData(this.shopid);
    },
  },
};
</script>

<style scoped lang="scss">
.like {
  padding: 0 16.19rem;
  margin-top: 2.19rem;
  display: flex;
  flex-direction: column;
  .goods-list {
    margin-top: 1.88rem;
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;

    .goods-item {
      width: 17.1rem;
      height: 25.88rem;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-right: 1.3333333%;
      margin-bottom: 2.38rem;
      cursor: pointer;

      &:nth-child(4n) {
        margin-right: 0;
      }

      .goods-img {
        width: 100%;
        height: 20.94rem;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;

        img {
          width: 16rem;
          height: 16rem;
          object-fit: contain;
        }
      }

      .goods-info {
        width: 17.1rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        margin-top: 1.5rem;

        .name {
          font-size: 1.13rem;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: rgba(0, 0, 0, 1);
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          width: 100%;
        }

        .price {
          font-size: 1.5rem;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: rgba(243, 64, 111, 1);
          margin-top: 0.75rem;
        }
      }
    }
  }
}
.more {
  font-size: 1rem;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(102, 102, 102, 1);
  line-height: 1.95rem;
  text-align: center;
  margin: 3.13rem auto;
  cursor: pointer;
}
</style>
